package com.raining.inningz.database.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.SqlUtils;
import de.greenrobot.dao.internal.DaoConfig;
import de.greenrobot.dao.query.Query;
import de.greenrobot.dao.query.QueryBuilder;

import com.raining.inningz.database.dao.BowlingSummary;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table BOWLING_SUMMARY.
*/
public class BowlingSummaryDao extends AbstractDao<BowlingSummary, Long> {

    public static final String TABLENAME = "BOWLING_SUMMARY";

    /**
     * Properties of entity BowlingSummary.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property BallNum = new Property(1, Long.class, "ballNum", false, "BALL_NUM");
        public final static Property BowlerNum = new Property(2, Long.class, "bowlerNum", false, "BOWLER_NUM");
        public final static Property InProgressSw = new Property(3, Long.class, "inProgressSw", false, "IN_PROGRESS_SW");
        public final static Property NoBalls = new Property(4, Long.class, "noBalls", false, "NO_BALLS");
        public final static Property Position = new Property(5, Long.class, "position", false, "POSITION");
        public final static Property Runs = new Property(6, Long.class, "runs", false, "RUNS");
        public final static Property ScoreLineHistorySw = new Property(7, Long.class, "scoreLineHistorySw", false, "SCORE_LINE_HISTORY_SW");
        public final static Property Undef1 = new Property(8, Long.class, "undef1", false, "UNDEF1");
        public final static Property UploadedSw = new Property(9, Long.class, "uploadedSw", false, "UPLOADED_SW");
        public final static Property Wickets = new Property(10, Long.class, "wickets", false, "WICKETS");
        public final static Property Wides = new Property(11, Long.class, "wides", false, "WIDES");
        public final static Property BowlingHistoryForScoreLine = new Property(12, Long.class, "bowlingHistoryForScoreLine", false, "BOWLING_HISTORY_FOR_SCORE_LINE");
        public final static Property Inning = new Property(13, Long.class, "inning", false, "INNING");
        public final static Property Maidens = new Property(14, Float.class, "maidens", false, "MAIDENS");
        public final static Property Overs = new Property(15, Float.class, "overs", false, "OVERS");
        public final static Property Undef2 = new Property(16, String.class, "undef2", false, "UNDEF2");
        public final static Property Undef3 = new Property(17, String.class, "undef3", false, "UNDEF3");
        public final static Property Undef4 = new Property(18, String.class, "undef4", false, "UNDEF4");
        public final static Property MatchInningBowlingSummaryId = new Property(19, Long.class, "matchInningBowlingSummaryId", false, "MATCH_INNING_BOWLING_SUMMARY_ID");
    };

    private DaoSession daoSession;

    private Query<BowlingSummary> matchInnings_BowlingSummaryListQuery;

    public BowlingSummaryDao(DaoConfig config) {
        super(config);
    }
    
    public BowlingSummaryDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "'BOWLING_SUMMARY' (" + //
                "'_id' INTEGER PRIMARY KEY ," + // 0: id
                "'BALL_NUM' INTEGER," + // 1: ballNum
                "'BOWLER_NUM' INTEGER," + // 2: bowlerNum
                "'IN_PROGRESS_SW' INTEGER," + // 3: inProgressSw
                "'NO_BALLS' INTEGER," + // 4: noBalls
                "'POSITION' INTEGER," + // 5: position
                "'RUNS' INTEGER," + // 6: runs
                "'SCORE_LINE_HISTORY_SW' INTEGER," + // 7: scoreLineHistorySw
                "'UNDEF1' INTEGER," + // 8: undef1
                "'UPLOADED_SW' INTEGER," + // 9: uploadedSw
                "'WICKETS' INTEGER," + // 10: wickets
                "'WIDES' INTEGER," + // 11: wides
                "'BOWLING_HISTORY_FOR_SCORE_LINE' INTEGER," + // 12: bowlingHistoryForScoreLine
                "'INNING' INTEGER," + // 13: inning
                "'MAIDENS' REAL," + // 14: maidens
                "'OVERS' REAL," + // 15: overs
                "'UNDEF2' TEXT," + // 16: undef2
                "'UNDEF3' TEXT," + // 17: undef3
                "'UNDEF4' TEXT," + // 18: undef4
                "'MATCH_INNING_BOWLING_SUMMARY_ID' INTEGER);"); // 19: matchInningBowlingSummaryId
    }

    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'BOWLING_SUMMARY'";
        db.execSQL(sql);
    }

    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, BowlingSummary entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Long ballNum = entity.getBallNum();
        if (ballNum != null) {
            stmt.bindLong(2, ballNum);
        }
 
        Long bowlerNum = entity.getBowlerNum();
        if (bowlerNum != null) {
            stmt.bindLong(3, bowlerNum);
        }
 
        Long inProgressSw = entity.getInProgressSw();
        if (inProgressSw != null) {
            stmt.bindLong(4, inProgressSw);
        }
 
        Long noBalls = entity.getNoBalls();
        if (noBalls != null) {
            stmt.bindLong(5, noBalls);
        }
 
        Long position = entity.getPosition();
        if (position != null) {
            stmt.bindLong(6, position);
        }
 
        Long runs = entity.getRuns();
        if (runs != null) {
            stmt.bindLong(7, runs);
        }
 
        Long scoreLineHistorySw = entity.getScoreLineHistorySw();
        if (scoreLineHistorySw != null) {
            stmt.bindLong(8, scoreLineHistorySw);
        }
 
        Long undef1 = entity.getUndef1();
        if (undef1 != null) {
            stmt.bindLong(9, undef1);
        }
 
        Long uploadedSw = entity.getUploadedSw();
        if (uploadedSw != null) {
            stmt.bindLong(10, uploadedSw);
        }
 
        Long wickets = entity.getWickets();
        if (wickets != null) {
            stmt.bindLong(11, wickets);
        }
 
        Long wides = entity.getWides();
        if (wides != null) {
            stmt.bindLong(12, wides);
        }
 
        Long bowlingHistoryForScoreLine = entity.getBowlingHistoryForScoreLine();
        if (bowlingHistoryForScoreLine != null) {
            stmt.bindLong(13, bowlingHistoryForScoreLine);
        }
 
        Long inning = entity.getInning();
        if (inning != null) {
            stmt.bindLong(14, inning);
        }
 
        Float maidens = entity.getMaidens();
        if (maidens != null) {
            stmt.bindDouble(15, maidens);
        }
 
        Float overs = entity.getOvers();
        if (overs != null) {
            stmt.bindDouble(16, overs);
        }
 
        String undef2 = entity.getUndef2();
        if (undef2 != null) {
            stmt.bindString(17, undef2);
        }
 
        String undef3 = entity.getUndef3();
        if (undef3 != null) {
            stmt.bindString(18, undef3);
        }
 
        String undef4 = entity.getUndef4();
        if (undef4 != null) {
            stmt.bindString(19, undef4);
        }
    }

    @Override
    protected void attachEntity(BowlingSummary entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    /** @inheritdoc */
    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    /** @inheritdoc */
    @Override
    public BowlingSummary readEntity(Cursor cursor, int offset) {
        BowlingSummary entity = new BowlingSummary( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1), // ballNum
            cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2), // bowlerNum
            cursor.isNull(offset + 3) ? null : cursor.getLong(offset + 3), // inProgressSw
            cursor.isNull(offset + 4) ? null : cursor.getLong(offset + 4), // noBalls
            cursor.isNull(offset + 5) ? null : cursor.getLong(offset + 5), // position
            cursor.isNull(offset + 6) ? null : cursor.getLong(offset + 6), // runs
            cursor.isNull(offset + 7) ? null : cursor.getLong(offset + 7), // scoreLineHistorySw
            cursor.isNull(offset + 8) ? null : cursor.getLong(offset + 8), // undef1
            cursor.isNull(offset + 9) ? null : cursor.getLong(offset + 9), // uploadedSw
            cursor.isNull(offset + 10) ? null : cursor.getLong(offset + 10), // wickets
            cursor.isNull(offset + 11) ? null : cursor.getLong(offset + 11), // wides
            cursor.isNull(offset + 12) ? null : cursor.getLong(offset + 12), // bowlingHistoryForScoreLine
            cursor.isNull(offset + 13) ? null : cursor.getLong(offset + 13), // inning
            cursor.isNull(offset + 14) ? null : cursor.getFloat(offset + 14), // maidens
            cursor.isNull(offset + 15) ? null : cursor.getFloat(offset + 15), // overs
            cursor.isNull(offset + 16) ? null : cursor.getString(offset + 16), // undef2
            cursor.isNull(offset + 17) ? null : cursor.getString(offset + 17), // undef3
            cursor.isNull(offset + 18) ? null : cursor.getString(offset + 18) // undef4
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, BowlingSummary entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setBallNum(cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1));
        entity.setBowlerNum(cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2));
        entity.setInProgressSw(cursor.isNull(offset + 3) ? null : cursor.getLong(offset + 3));
        entity.setNoBalls(cursor.isNull(offset + 4) ? null : cursor.getLong(offset + 4));
        entity.setPosition(cursor.isNull(offset + 5) ? null : cursor.getLong(offset + 5));
        entity.setRuns(cursor.isNull(offset + 6) ? null : cursor.getLong(offset + 6));
        entity.setScoreLineHistorySw(cursor.isNull(offset + 7) ? null : cursor.getLong(offset + 7));
        entity.setUndef1(cursor.isNull(offset + 8) ? null : cursor.getLong(offset + 8));
        entity.setUploadedSw(cursor.isNull(offset + 9) ? null : cursor.getLong(offset + 9));
        entity.setWickets(cursor.isNull(offset + 10) ? null : cursor.getLong(offset + 10));
        entity.setWides(cursor.isNull(offset + 11) ? null : cursor.getLong(offset + 11));
        entity.setBowlingHistoryForScoreLine(cursor.isNull(offset + 12) ? null : cursor.getLong(offset + 12));
        entity.setInning(cursor.isNull(offset + 13) ? null : cursor.getLong(offset + 13));
        entity.setMaidens(cursor.isNull(offset + 14) ? null : cursor.getFloat(offset + 14));
        entity.setOvers(cursor.isNull(offset + 15) ? null : cursor.getFloat(offset + 15));
        entity.setUndef2(cursor.isNull(offset + 16) ? null : cursor.getString(offset + 16));
        entity.setUndef3(cursor.isNull(offset + 17) ? null : cursor.getString(offset + 17));
        entity.setUndef4(cursor.isNull(offset + 18) ? null : cursor.getString(offset + 18));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(BowlingSummary entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(BowlingSummary entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "bowlingSummaryList" to-many relationship of MatchInnings. */
    public List<BowlingSummary> _queryMatchInnings_BowlingSummaryList(Long matchInningBowlingSummaryId) {
        synchronized (this) {
            if (matchInnings_BowlingSummaryListQuery == null) {
                QueryBuilder<BowlingSummary> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.MatchInningBowlingSummaryId.eq(null));
                matchInnings_BowlingSummaryListQuery = queryBuilder.build();
            }
        }
        Query<BowlingSummary> query = matchInnings_BowlingSummaryListQuery.forCurrentThread();
        query.setParameter(0, matchInningBowlingSummaryId);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getPlayerDao().getAllColumns());
            builder.append(" FROM BOWLING_SUMMARY T");
            builder.append(" LEFT JOIN PLAYER T0 ON T.'BOWLER_NUM'=T0.'_id'");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected BowlingSummary loadCurrentDeep(Cursor cursor, boolean lock) {
        BowlingSummary entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        Player player = loadCurrentOther(daoSession.getPlayerDao(), cursor, offset);
        entity.setPlayer(player);

        return entity;    
    }

    public BowlingSummary loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<BowlingSummary> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<BowlingSummary> list = new ArrayList<BowlingSummary>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<BowlingSummary> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<BowlingSummary> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
